import 'package:bwt_ad_flutter_example/page/BannerAdPage.dart';
import 'package:bwt_ad_flutter_example/page/NativeAdPage.dart';
import 'package:bwt_ad_flutter_example/page/ServiceAdPage.dart';
import 'package:bwt_ad_flutter_example/page/SplashAdPage.dart';
import 'package:bwt_ad_flutter_example/widget/SimpleWidget.dart';
import 'package:flutter/material.dart';

import 'InterstitialAdPage.dart';
import 'NativeExpressAdListDataPage.dart';
import 'NativeExpressAdPage.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<StatefulWidget> createState() => _MainState();
}

class _MainState extends State<MainPage> {
  static const _adTypeList = [
    {
      'type': 0,
      'title': '开屏广告',
    },
    {
      'type': 1,
      'title': '插屏广告',
    },
    {
      'type': 2,
      'title': '信息流模板广告',
    },
    {
      'type': 3,
      'title': '信息流模板广告-列表场景',
    },
    {
      'type': 4,
      'title': 'banner广告',
    },
    {
      'type': 5,
      'title': '服务类型广告',
    },
    {
      'type': 6,
      'title': '信息流自渲染',
    }
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("BwtAdFlutter"),
      ),
      body: ListView.builder(
          padding: const EdgeInsets.symmetric(vertical: 32, horizontal: 16),
          itemCount: _adTypeList.length,
          itemBuilder: (context, index) {
            Map<String, dynamic> adType = _adTypeList[index];
            return SimpleWidget(
              width: double.infinity,
              height: 64,
              alignment: Alignment.center,
              onTap: () {
                _onItemClick(adType['type'], context);
              },
              child: Text(
                adType['title'] ?? '',
                style: const TextStyle(
                    color: Colors.black,
                    fontSize: 20,
                    fontWeight: FontWeight.bold),
              ),
            );
          }),
    );
  }

  /// List item 点击
  void _onItemClick(adType, BuildContext context) {
    if (0 == adType) {
      _push(context, const SplashAdPage());
    } else if (1 == adType) {
      _push(context, const InterstitialAdPage());
    } else if (2 == adType) {
      _push(context, const NativeExpressAdPage());
    } else if (3 == adType) {
      _push(context, const NativeExpressAdListDataPage());
    } else if (adType == 4) {
      _push(context, const BannerAdPage());
    } else if (adType == 5) {
      _push(context, const ServiceAdPage());
    } else if (adType == 6) {
      _push(context, const NativeAdPage());
    }
  }

  /// 跳转到指定页面
  void _push(BuildContext context, Widget widget) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return widget;
    }));
  }
}
